package com.obrempon.credit.market.common

import android.content.Context
import android.view.Gravity
import android.view.WindowManager
import com.obrempon.credit.market.ObremponApp
import com.obrempon.credit.market.R
import com.obrempon.credit.market.base.BaseDialog
import com.obrempon.credit.market.event.ObremponEventPointHelper
import com.obrempon.credit.market.request.HttpHelper
import com.obrempon.credit.market.request.IHttpCallback
import com.obrempon.credit.market.utils.ObremponDensityUtil
import com.obrempon.module.easyhttp.config.IRequestApi
import org.json.JSONObject

/**
 * CommonDialog
 * @Author wwdeng
 * @CreateTime: 2024/11/28 10:32
 * @Annotation：
 */
abstract class CommonDialog(context: Context): BaseDialog(context) {

    override fun initObremponWindow() {
        val params = window!!.attributes
        params.width = ObremponApp.app.getPhoneWidth() - ObremponDensityUtil.dp2px(32f)
        params.height = WindowManager.LayoutParams.WRAP_CONTENT
        window!!.attributes = params
        window!!.setWindowAnimations(R.style.dialog_scale_anim) //修改动画样式
        window!!.setGravity(Gravity.CENTER)
    }

    /**
     * post
     */
    fun doPostAction(httpUrl: String, params: JSONObject, callback: IHttpCallback){
        HttpHelper.post(httpUrl,params,object : IHttpCallback{
            override fun onRequestSuccess(data: JSONObject) {
                callback.onRequestSuccess(data)
            }

            override fun onRequestError(error: JSONObject) {
                callback.onRequestError(error)
                showObremponToast(error.optString("errorMsg"))
            }

            override fun onRequestFinish() {
                callback.onRequestFinish()
            }
        })
    }

    /**
     * post
     */
    fun doPostAction(api: IRequestApi, callback: IHttpCallback){
        HttpHelper.post(api,object : IHttpCallback{
            override fun onRequestSuccess(data: JSONObject) {
                callback.onRequestSuccess(data)
            }

            override fun onRequestError(error: JSONObject) {
                callback.onRequestError(error)
                showObremponToast(error.optString("errorMsg"))
            }

            override fun onRequestFinish() {
                callback.onRequestFinish()
            }
        })
    }

    /**
     * 添加埋点
     */
    fun addEventPointRequestAction(eventName: String, eventRemark: String = "") {
        ObremponEventPointHelper.addEventPointRequestAction(eventName,eventRemark)
    }

    /**
     * getString
     */
    fun getString(strId: Int): String{
        return ObremponApp.app.getString(strId)
    }

    /**
     * getColor
     */
    fun getColor(color: Int): Int{
        return ObremponApp.app.getColor(color)
    }

    override fun show() {
        super.show()
        HttpHelper.setTag(this)
    }

    override fun dismiss() {
        super.dismiss()
        HttpHelper.cancelTag(this)
    }
}